package com.shuzixiangcun.filter;

import com.shuzixiangcun.entity.Users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 描述
 *
 * @version 1.0
 * @Title:
 * @author: YANBOWEN
 * @Description:
 * @date 2024/07/08 08:41:04
 */
public class RecruitFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpSession session=request.getSession();
        servletResponse.setCharacterEncoding("utf-8");
        //校验用户是否登录
        Users loginUser = (Users) session.getAttribute("USER");
        /*if (loginUser == null) {
            //return ApiRestResponse.error(IlyrkExceptionEnum.NEED_LOGIN);
            PrintWriter out=new HttpServletResponseWrapper((HttpServletResponse) servletResponse).getWriter();
            out.write("{\n"
                    + "    \"status\": 10008,\n"
                    + "    \"msg\": \"NEED_LOGIN\",\n"
                    + "    \"data\": null\n"
                    + "}");
            out.flush();
            out.close();
            return;
        }*/
        //校验管理员
        if(loginUser.getRole()==2){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            PrintWriter out = new HttpServletResponseWrapper(
                    (HttpServletResponse) servletResponse).getWriter();
            out.write("{\n"
                    + "    \"status\": 10010,\n"
                    + "    \"msg\": \"尚未企业认证\",\n"
                    + "    \"data\": null\n"
                    + "}");
            out.flush();
            out.close();
        }
    }
}
